<%@ page language="java" import="java.util.*,java.lang.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ include  file="../jsp/userControl.jsp"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
	Calendar calendar = Calendar.getInstance();
	String strSchoolYear1 = "";
	String strSchoolYear2 = "";
	int intYear = calendar.get(Calendar.YEAR);
	int intMonth = calendar.get(Calendar.MONTH);
	if (intMonth <= 6) {
		strSchoolYear1 = String.valueOf(intYear - 1) + "-"
				+ String.valueOf(intYear);
		strSchoolYear2 = String.valueOf(intYear) + "-"
				+ String.valueOf(intYear + 1);
	} else {
		strSchoolYear1 = String.valueOf(intYear) + "-"
				+ String.valueOf(intYear + 1);
		strSchoolYear2 = String.valueOf(intYear) + "-"
				+ String.valueOf(intYear + 1);
	}
	request.setAttribute("strSchoolYear1", strSchoolYear1);
	request.setAttribute("strSchoolYear2", strSchoolYear2);
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 5.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<link rel="stylesheet" type="text/css" href="css/classAdd.css" />
<script>
//数据检验：文件上传信息
function form_submit(form) {
	recoverFlag();
	if (checkCourseName(form) &checkSchoolYear(form) &checkTerm(form) &checkCredit(form) &checkCourseStart(form) & checkCourseEnd(form)) {
		return true;
	} else {
		return false;
	}
}

//重置按钮
function recoverFlag() {
	var span1 = document.getElementById("courseNameSpanId");
	var span2 = document.getElementById("schoolYearSpanId");
	var span3 = document.getElementById("termSpanId");
	var span4 = document.getElementById("creditSpanId");
	var span5 = document.getElementById("courseStartSpanId");
	var span6 = document.getElementById("courseEndSpanId");
	span1.innerHTML = "*";
	span2.innerHTML = "*";
	span3.innerHTML = "*";
	span4.innerHTML = "*";
	span5.innerHTML = "*";
	span6.innerHTML = "*";
}

//数据校验：班级名
function checkCourseName(form){//必填，1-20个字母数字汉字或下划线
	var courseName = form.courseName;
	var courseName_value = form.courseName.value;
	var span = document.getElementById("courseNameSpanId");
	var regex = /^[a-zA-Z0-9_\u4e00-\u9fa5]{1,20}$/;
	if (regex.test(courseName_value)) {
		span.innerHTML="<img src='images/ok.gif'/>";
		return true;
	} else {
		span.innerHTML = "<img src='images/no.gif'/><font color='red'>必填,且由1-20个汉字、字母、数字、下划线组成</font>";
		courseName.focus();
		return false;
	}
}

//数据校验：学年
function checkSchoolYear(form){//必选
	var schoolYear = form.schoolYear;
	var schoolYear_value = form.schoolYear.value;
	var span = document.getElementById("schoolYearSpanId");
	if (schoolYear_value == -1) {
		span.innerHTML = "<img src='images/no.gif'/><font color='red'>必选</font>";
		schoolYear.focus();
		return false;
	} else {	
		span.innerHTML="<img src='images/ok.gif'/>";
		return true;
	}
}

//数据校验：学期
function checkTerm(form){//必选
	var term = form.term;
	var term_value = form.term.value;
	var span = document.getElementById("termSpanId");
	if (term_value == -1) {
		span.innerHTML = "<img src='images/no.gif'/><font color='red'>必选</font>";
		term.focus();
		return false;
	} else {	
		span.innerHTML="<img src='images/ok.gif'/>";
		return true;
	}
}

//数据校验：学分
function checkCredit(form){//必填，且为1-20的整数
	var credit = form.credit;
	var credit_value = form.credit.value;
	var span = document.getElementById("creditSpanId");
	var regex = /^[1-9]\d*$/;
	if (regex.test(credit_value) & parseInt(credit_value)<21 & (credit_value != "")) {
		span.innerHTML="<img src='images/ok.gif'/>";
		return true;
	} else {		
		span.innerHTML = "<img src='images/no.gif'/><font color='red'>必填，且为1-20的数字</font>";
		credit.focus();
		return false;
	}
}

//数据校验：课程开始周
function checkCourseStart(form){//必填，且为1-20的整数
	var courseStart = form.courseStart;
	var courseStart_value = form.courseStart.value;
	var span = document.getElementById("courseStartSpanId");
	var regex = /^[1-9]\d*$/;
	if (regex.test(courseStart_value) & parseInt(courseStart_value)<21 & (courseStart_value != "")) {
		span.innerHTML="<img src='images/ok.gif'/>";
		return true;
	} else {		
		span.innerHTML = "<img src='images/no.gif'/><font color='red'>必填，且为1-20的数字</font>";
		courseStart.focus();
		return false;
	}
}

//数据校验：课程结束周
function checkCourseEnd(form){//必填，且为1-20的整数
	var courseEnd = form.courseEnd;
	var courseEnd_value = form.courseEnd.value;
	var span = document.getElementById("courseEndSpanId");
	var regex = /^[1-9]\d*$/;
	if (regex.test(courseEnd_value) & parseInt(courseEnd_value)<21 & (courseEnd_value != "")) {
		span.innerHTML="<img src='images/ok.gif'/>";
		return true;
	} else {		
		span.innerHTML = "<img src='images/no.gif'/><font color='red'>必填，且为1-20的数字</font>";
		courseEnd.focus();
		return false;
	}
}
</script>


</head>

<body>
	<form id="classAdd" class="contact_form"
		action="classAdd"
		method="post" name="contact_form" onsubmit="return form_submit(classAdd);">
		<ul>
			<li>
				<h2>新增班级</h2>
			</li>
			<li>
				<label for="name">班级名：</label> 
				<input type="text"
						name="courseName" maxlength="20" /> <span id="courseNameSpanId">*</span></li>
			<li>
				<label for="name">学年：</label> 
				<select name="schoolYear">
							<option value="-1">请选择学年</option>
							<option value="${strSchoolYear1}">${strSchoolYear1}</option>
							<option value="${strSchoolYear2}">${strSchoolYear2}</option>
					</select><span id="schoolYearSpanId">*</span></li>
			<li>
				<label for="name">学期：</label> 
				<select name="term">
							<option value="-1">请选择学期</option>
							<option value="上">上</option>
							<option value="下">下</option>
					</select><span id="termSpanId">*</span></li>
			<li>
				<label for="name">学分：</label> 
				<input type="text" name="credit"
						maxlength="20" /> <span id="creditSpanId">*</span></li>
			<li>
				<label for="message">课程开始周：</label> 
				<input type="text"
						name="courseStart" maxlength="20" /> <span id="courseStartSpanId">*</span>
			</li>
			<li>
				<label for="name">课程结束周：</label> 
				<input type="text"
						name="courseEnd" maxlength="20" /> <span id="courseEndSpanId">*</span></li>
			<li>
				<button class="submit" type="submit">新增</button>
			</li>
		</ul>
	</form>
</body>
</html>
